package com.yuexunit.finance.member.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.member.entity.EnterpriseCodeEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.StringUtil;

/**
 * @author wenwenlong
 */
@Implements(EnterpriseCodeService.class)
public class EnterpriseCodeServiceImpl implements EnterpriseCodeService {

	@Override
	@Transactional
	public Long createEnterpriseCode(EnterpriseCodeEntity entity) {
		PersistUtil.insert(entity);
		return null;
	}
	
	@Override
	@Transactional
	public void updateEnterpriseCode(EnterpriseCodeEntity entity) {
		PersistUtil.update(entity);
	}

	@Override
	public EnterpriseCodeEntity inquireEnterpriseCodeDetail(String enterpriseCode, Long enterpriseId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseCodeEntity.class);
		if (StringUtil.isNotBlank(enterpriseCode)) {
			criteria.add(Restrictions.eq("enterpriseCode", enterpriseCode));
		}
		
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		
		List<EnterpriseCodeEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	@Override
	public EnterpriseCodeEntity inquireEnterpriseCodeDetail(Long enterpriseId) {
		return PersistUtil.get(EnterpriseCodeEntity.class, enterpriseId);
	}
}
